/***
 * Copyright (c) 2010 readyState Software Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package net.kts.story.map.overlay;

import java.net.URL;
import net.kts.story.menu.StoryMenu;
import net.kts.transrate.TranslateService;
import twitter4j.GeoLocation;
import twitter4j.MediaEntity;
import twitter4j.Status;
import twitter4j.Tweet;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;


public class StoryItemizedOverlayExtends extends StoryItemizedOverlay<OverlayItem> 
				{
	private StoryMenu storyMenu;
	
	public StoryItemizedOverlayExtends(Drawable defaultMarker, MapView mapView, StoryMenu storyMenu) {
		super(boundCenter(defaultMarker), mapView);
		this.storyMenu = storyMenu;
	}
	
	public void addTweetOverlay(Tweet tweet, GeoLocation location) {
		GeoPoint point = new GeoPoint(
				(int) (location.getLatitude() * 1000000),
				(int) (location.getLongitude() * 1000000));
		
		String text = tweet.getText().split("#KoreaTravel")[1];
		
		StoryTweetOverlayItem overlayItem = new StoryTweetOverlayItem(
				point, 
				tweet.getFromUserName(), 
				text);
		
		overlayItem.setProfileImageURL(tweet.getProfileImageUrl());
		MediaEntity[] mediaEntities = tweet.getMediaEntities();
		if(mediaEntities != null && mediaEntities.length != 0){
			for (MediaEntity mediaEntity : mediaEntities) {
				URL mediaURL = mediaEntity.getMediaURL();
				if(mediaURL != null);
					overlayItem.setAttachImageURL(mediaURL);
			}
		}
		addOverlay(overlayItem);
	}

	public void addOverlay(StoryTweetOverlayItem overlay) {
	    tweetOverlays.add(overlay);
	    populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return tweetOverlays.get(i);
	}

	@Override
	public int size() {
		return tweetOverlays.size();
	}

	@Override
	protected boolean onBalloonTap(int index) {
		
		StoryTweetOverlayItem item = (StoryTweetOverlayItem) tweetOverlays.get(index);
		String translate = TranslateService.translate(item.getSnippet());
		URL attachImageURL = item.getAttachImageURL();
		storyMenu.showMessageView(item.getTitle(), translate, attachImageURL);
		
		return true;
	}

	public void addTweetOverlay(Status homeLine, GeoLocation location) {
		GeoPoint point = new GeoPoint(
				(int) (location.getLatitude() * 1000000),
				(int) (location.getLongitude() * 1000000));
		
		String text = homeLine.getText().split("#KoreaTravel")[1];
		
		StoryTweetOverlayItem overlayItem = new StoryTweetOverlayItem(
				point, 
				homeLine.getUser().getName(), 
				text);
		
		URL profileImageURL = homeLine.getUser().getProfileImageURL();
		overlayItem.setProfileImageURL(profileImageURL.getPath());
		MediaEntity[] mediaEntities = homeLine.getMediaEntities();
		if(mediaEntities != null && mediaEntities.length != 0){
			for (MediaEntity mediaEntity : mediaEntities) {
				URL mediaURL = mediaEntity.getMediaURL();
				if(mediaURL != null);
					overlayItem.setAttachImageURL(mediaURL);
			}
		}
		addOverlay(overlayItem);
		
	}

	


}
